//
//  BSSServiceSwitchTableViewCell.m
//  PreschoolEducation
//
//  Created by 李传铎 on 2020/11/15.
//  Copyright © 2020 李传铎. All rights reserved.
//

#import "BSSServiceSwitchTableViewCell.h"

@implementation BSSServiceSwitchTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        [self createUI];
        [self layoutUI];
    }
    return self;
}

- (void)createUI {
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.line];
    [self.contentView addSubview:self.switchBtn];
}

- (void)layoutUI {

    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__kSize(22));
        make.top.mas_equalTo(__kSize(12));
    }];
  
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLab.mas_bottom).mas_offset(__kSize(12));
        make.left.mas_equalTo(__kSize(22));
        make.right.mas_equalTo(__kSize(- 22));
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(self.contentView);
    }];
    [_switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(25);
        make.centerY.mas_equalTo(_titleLab.mas_centerY);
    }];
    
    
}
- (void)setModel:(BSSEnterpriseCertificationModel *)model{
    _model = model;
    if ([model.value isEqualToString:@"1"]) {
        self.switchBtn.on = YES;
        
    }else{
        self.switchBtn.on = NO;
    }
}

#pragma mark - action
- (void)switchAction:(UISwitch *)switchButton{
//    switchButton.on = !switchButton.on;
    if (switchButton.on) {
        self.model.value = @"1";
    }else{
        self.model.value = @"";
    }
}
#pragma mark - setter & getter

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.textColor = __kColor(51, 51, 51);
        _titleLab.font = fontCount(14);
    }
    return _titleLab;
}
- (UISwitch *)switchBtn{
    if (!_switchBtn) {
        _switchBtn = [[UISwitch alloc] init];
        _switchBtn.onTintColor = colorCode(@"#F34B4D");
        [_switchBtn addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _switchBtn;
}

- (UIView *)line {
    if (!_line) {
        _line = [UIView new];
        _line.backgroundColor = __kColor(238, 238, 238);
    }
    return _line;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
